import type { App } from "vue";

import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
  NavigationGuardNext,
  RouteLocationNormalized,
} from "vue-router";
import { useUserStoreHook } from "@/store/modules/user"; // 导入 user store hook
export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login", //登录
    component: () => import("@/views/login/index.vue"),
    //不在菜单栏显示
    meta: { hidden: true, requiresAuth: false },
  },
  {
    path: "/user",
    component: () => import("@/views/user/index.vue"),
    meta: { hidden: true, requiresAuth: true },
  },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    meta: { requiresAuth: true },
    children: [
      {
        path: "dashboard", //首页
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          requiresAuth: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true, requiresAuth: false },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true, requiresAuth: false },
      },
    ],
  },
  {
    name: "resource",
    path: "/resource",
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/resource/index.vue"),
        name: "Resource",
        meta: { title: "资源管理", requiresAuth: true },
      },
    ],
  },
  {
    name: "student",
    path: "/student",
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: "index", // 学生管理
        component: () => import("@/views/student/index.vue"),
        name: "Student",
        meta: {
          title: "学生管理",
          requiresAuth: true,
        },
      },
    ],
  },
  {
    name: "subject",
    path: "/subject",
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: "index", // 课程管理
        component: () => import("@/views/subject/index.vue"),
        name: "Subject",
        meta: {
          title: "课程管理",
          requiresAuth: true,
        },
      },
    ],
  },
  {
    name: "test",
    path: "/test",
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: "index",
        component: () => import("@/views/test/index.vue"),
        name: "Test",
        meta: {
          title: "题目管理",
          icon: "document",
          requiresAuth: true,
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});
// 全局前置守卫
router.beforeEach(
  (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    const requiresAuth = to.meta.requiresAuth === true;
    const userStore = useUserStoreHook(); // 获取 user store 实例

    if (requiresAuth && !userStore.isLogin) {
      next("/login"); // 如果需要认证且未登录，跳转到登录页
    } else {
      next(); // 否则继续导航
    }
  }
);
// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
